/*
 *  priceEntries.c
 *  PriceCompareDS
 *
 *  Created by Stacy Haponik on 5/27/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
#include <stdio.h>
#include <string.h>
#include <fat.h>
//#include <curses.h>
#include <nds.h>
#ifndef utilities.c
#define utilities.c
#endif

/*

int inputEntry(void){		//append a new entry to the end of entries.txt, based on user input
	iprintf();
	Date	Store		Item Type	Item	Size Unit	Price Price per unit	Sale?	Coupon?	Notes
	//iprintf("Today? \n");
	iprintf("Store? \n");
	iprintf("Item Type? \n");
	iprintf("Item? \n");
	iprintf("Price? \n");
	iprintf("Size? \n");
	iprintf("Unit? \n");
	iprintf("Sale? \n");
	iprintf("Coupon? \n");
	iprintf("Notes? \n");
	}
	
*/

/*

Date	Store		Item Type	Item	Size Unit	Price Price per unit	Sale?	Coupon?	Notes
char[10]	Date			//TODO: figure out how to use the date types.
char[20]	Store
char[10]	Item Type
char[20]	Item Name
int			Size
char[3]		Unit
double		Price
bool		Sale?	
bool		Coupon?	
char[200]	Notes
*/
//Item	Size Unit	Price	Sale?	Coupon?	Notes

typedef struct {
	char date[10];
	char store[20];
	char itemType[10];
	char itemName[20];
	int	size;
	char unit[3];
	double price;
	int sale;
	int coupon;
	char notes[200];
	} aPrice;

/*

int maxNameSize=20;
int maxLocationSize=100;
int maxNotesSize=100;
int priceCount=0;

typedef struct{
      char name[20];
      char location[100];
	  char notes[2];
} myEntries;

myEntries arrayOfEntries[20];										//FIX THIS: am getting stuck on figuring out how to set the size of this array appropriately, so am making it size 20.


int numberOfEntries(void){					//determines how many stores are in stores.txt
      FILE *myFile;
	  myFile=fopen("fat:stores.txt", "r"); 
	  	if (!myFile){
		iprintf("Couldn't open stores.txt for reading\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3");
		return 0;
		}
      else {
         return numberOfLinesInFile(myFile)-1;
      }
	}

int readInEntries(void){							//copy all stores into memory
	FILE *myFile;
	myFile=fopen("fat:stores.txt", "r"); 
	if (!myFile){
		iprintf("Couldn't open stores.txt for reading.");
		return 0;
		}
	else {
		priceCount=numberOfEntries();
		}

	char myChar;
	myFile=fopen("fat:stores.txt", "r");
	//myEntries arrayOfEntries[storeCount];			//make an array of store structs, of size to fit all the stores
	int j=0;
		myChar = fgetc(myFile);
		while ( myChar!='\n'){		//loop through till the end of the first line, since it's just a header
			myChar = fgetc(myFile);
			}
		for (j=0; j < priceCount; j++){			//loop until all stores are added to the arrayOfEntries
			int i=0;
			int exit=0;
		//	iprintf("\nreadin loop\n");
			for (i=0; i < maxNameSize - 1&&exit==0; i++){		//add the store name to the struct
				char myString[maxNameSize];
//				int k=0;
//				for(k=0;k<maxNameSize;k++){
//						myString[k]='0';
//						}

				myChar = fgetc(myFile);
				if ((myChar == '\t')||( myChar=='\n')){
					myString[i]='\0';
					strcpy(arrayOfEntries[j].name,myString);
//					iprintf("A Name: %s\n",myString);
					exit=1;
					}
				else myString[i]=myChar;
				}	//add name to struct
			exit=0;
			for (i=0; i < maxLocationSize - 1&&exit==0; i++){		//add the store location to the struct
				char myString[maxLocationSize];
//				int k=0;
//				for(k=0;k<maxLocationSize;k++){
//					myString[k]='0';
//					}				

				myChar = fgetc(myFile);
		//		iprintf("adding location\n");
				if ((myChar == '\t')||( myChar=='\n')){
					myString[i]='\0';
					strcpy(arrayOfEntries[j].location,myString);
					exit=1;
					}
				}	//add location to struct
			exit=0;
			for (i=0; i < maxNotesSize - 1&&exit==0; i++){		//add the store notes to the struct
				char myString[maxNotesSize];
//				int k=0;
//				for(k=0;k<maxNameSize;k++){
//					myString[k]='0';
//					}
//
					myChar = fgetc(myFile);
		//		iprintf("adding notes\n");
				if ((myChar == '\t')||( myChar=='\n')){
					myString[i]='\0';
					strcpy(arrayOfEntries[j].notes,myString);
					exit=1;
					}
				}	//add notes to struct
			}	
	fclose(myFile);
	return 0;
	}		//copy all stores into memory

int printEntries(void){
	readInEntries();
//	iprintf("EntryCount according to printEntries: %i\n",storeCount);
	int i=0;
	for (i=0; i<priceCount; i++){	//loop until we reach the last store
		iprintf("%d: %s\n",(i+1),arrayOfEntries[i].name);
	}
	return 0;
	}

/*
void keyboardGetStringAndPrintChar(char * buffer, int maxLen){			//"borrowed" from keyboard.h, then modified to print the characters input
	char *end = buffer + maxLen;
 	char c;
 
	while(buffer < end){
		c = (char)keyboardGetChar(); 
		if(c == 10){				//magic number comes from keyboard.h-- equivalent to the "enter" key.
			break;
			}
//		iprintf("%c",c);
		*buffer++ = c;
		}
	*buffer = 0;
	}	


int addEntry(void){		//append a new store to the end of stores.txt, based on user input
	keyboardDemoInit();
	keyboardShow();
//	int key = keyboardUpdate();

	FILE *myFile=fopen("fat:stores.txt", "a"); 
	int bufferSize=maxNameSize+maxLocationSize+maxNotesSize+4;
	char buffer[bufferSize];
	
	iprintf("Entry Name? \n");
	keyboardGetStringAndPrintChar (buffer, maxNameSize);
	char name[maxNameSize];
	strncpy(name,buffer,maxNameSize);

	iprintf("Location? \n");
	keyboardGetStringAndPrintChar (buffer, maxLocationSize);
	char location[maxLocationSize];
	strncpy(location,buffer,maxLocationSize);

	iprintf("Notes? \n");
	keyboardGetStringAndPrintChar (buffer, maxNotesSize);
	char notes[maxNotesSize];
	strncpy(notes,buffer,maxNotesSize);
	
	fprintf(myFile,"\n%s\t%s\t%s",name,location,notes);
	fclose(myFile);
	return 0;
	}
	
int nextEntry(){
	char Name[maxNameSize];
	char Location[maxLocationSize];
//	(void) scanf("%2d%f%*d %[0123456789]", &i, &x, name);
	return 0;
}


int removeEntry(void){
	return 0;
	}

int priceScreen(){
	printEntries();
	int addEntry=0;	//if true, then we should display the "add store" keyboard and questions.
	int i=0;		//iterator to store our spot in the "add store" buffer.
	int question=1;		//which question are we on?
	int maxQuestion=3;	//how many questions do I have?
	int prompt=0;		//is it time to ask a question?
	int answerLength=0;
	int maxAnswerLength=20;	//how long of an answer should the prompt accept?
	keyboardDemoInit();
//	keyboardHide();
//	keyboardShow();

	char buffer[maxNameSize+maxLocationSize+maxNotesSize+3];	//add store buffer-- size is maximum possible size for a line to be saved to stores.txt.
	
	while(1){		//loop until you get input
		scanKeys();
		int pressed = keysDown();   // buttons pressed this loop
		
		if ( pressed & KEY_A){
//			consoleClear();
			addEntry=1;
			prompt=1;
//			printEntries();
			keyboardShow();
			iprintf("Let's add a store!");
		}		//add a store
		if ( pressed & KEY_L){
			return 1;
			iprintf("Swapping to price entries screen");
			break;
		}		//swap to "price entries" screen
		if ( pressed & KEY_R){
			return 2;
			iprintf("Swapping to Calculator screen");
			break;
		}		//swap to calculator mode
		int key = keyboardUpdate();
//		iprintf("\nAdd Entry: %i, Prompt: %i Key: %c", addEntry,prompt,key); // It gets here

		if (addEntry==1){
//			consoleSelect(&bottomScreen);
			if (prompt==1){
				prompt=0;
				switch (question){
					case 1:
						iprintf("\nEntry: ");
						maxAnswerLength=maxNameSize;
						break;
					case 2:
						iprintf("\nLocation: ");
						maxAnswerLength=maxLocationSize;
						break;
					case 3:
						iprintf("\nNotes: ");
						maxAnswerLength=maxNotesSize;
						break;
					default:
						iprintf("\nOoops. I don't know how I got here.");
						break;
				}
			}			//print the appropriate prompt
			if (key>0&&key!=255){
//					iprintf("%c", key);
				if ((key==10)||(answerLength>=maxAnswerLength)){
					buffer[i]='\t';
					prompt=1;
					question++;
					i++;
					answerLength=0;
//					iprintf("Adding a tab");
					}		//if there's a return character
				else if(key>=32){
					buffer[i]=key;
					iprintf("%c",key);
					i++;
					answerLength++;
					}		//if there's a letter, number, or space
				else if(key==8){
					i--;
					answerLength--;
					iprintf("%c",key);
					}		//if there's a backspace character
				if (question>maxQuestion){
					i--;		//remove the trailing tab character
					buffer[i]='\0';
					question=1;
					addEntry=0;
					i=0;
					answerLength=0;
					FILE *myFile=fopen("fat:stores.txt", "a"); 
					fprintf(myFile,"\n%s",buffer);
					fclose(myFile);
					consoleClear();
					iprintf("\nAdded store: %s\n",buffer);
					printEntries();
					keyboardHide();
					}				//print to file, reset iterators
				key=0;
				}
//			consoleSelect(&topScreen);
			}		//loop to add a store  
		swiWaitForVBlank();
	}
}
*/